package com.lc.midi.keyEvent;

import com.lc.midi.mid.MidiException;

import java.util.ArrayList;
import java.util.List;

public class KeyEvents {
	private List<KeyEvent> keyEventList = new ArrayList<>();

    public List<KeyEvent> getKeyEventList() {
        return keyEventList;
    }

    public void setKeyEventList(List<KeyEvent> keyEventList) {
        this.keyEventList = keyEventList;
    }

    public void addKeyEvent(KeyEvent keyEvent){
        for (int i = 0; i < keyEventList.size(); i++){
            if (keyEventList.get(i).getTime() == keyEvent.getTime()){
                for (var key : keyEvent.getKeyList())
                    keyEventList.get(i).addKey(key);
                return;
            }
            if (keyEventList.get(i).getTime() > keyEvent.getTime()){
                keyEventList.add(i, keyEvent);
                return;
            }
        }
        keyEventList.add(keyEvent);
    }
    
    public void addKeyEvent(int time, int keyId, int instrumentId, int pass) throws MidiException {
    	MidiKey midiKey = new MidiKey();
    	midiKey.setKeyId(keyId);
    	midiKey.setInstrumentId(instrumentId);
    	midiKey.setPass(pass);
    	KeyEvent keyEvent = new KeyEvent();
    	keyEvent.setTime(time);
    	keyEvent.addKey(midiKey);
    	this.addKeyEvent(keyEvent);
    }
}
